package com.hedl.classroom.base.exception;

/**
 * 接口返回结果
 */
public class CommonResult<T> {
    /**
     * 响应编码,0为正常,-1错误
     */
    private int code;
    /**
     * 响应提示信息
     */
    private String msg;

    /**
     * 响应内容
     */
    private T result;

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public CommonResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResult(int code){
        this.code = code;
    }

    public CommonResult(){
        this(0,"seccess");
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "CommonResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", result=" + result +
                '}';
    }

    public static CommonResult result(){
        return new CommonResult(200);
    }

    public static CommonResult error(String msg){
        return new CommonResult(120409,msg);
    }

    /**
     * 错误信息的封装
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> validfail(String msg) {
        CommonResult<T> response = new CommonResult<T>();
        response.setCode(-1);
        response.setMsg(msg);
        return response;
    }
    public static <T> CommonResult<T> validfail(T result,String msg) {
        CommonResult<T> response = new CommonResult<T>();
        response.setCode(-1);
        response.setResult(result);
        response.setMsg(msg);
        return response;
    }



    /**
     * 添加正常响应数据（包含响应内容）
     *
     * @return RestResponse Rest服务封装相应数据
     */
    public static <T> CommonResult<T> success(T result) {
        CommonResult<T> response = new CommonResult<T>();
        response.setResult(result);
        return response;
    }
    public static <T> CommonResult<T> success(T result,String msg) {
        CommonResult<T> response = new CommonResult<T>();
        response.setResult(result);
        response.setMsg(msg);
        return response;
    }

    /**
     * 添加正常响应数据（不包含响应内容）
     *
     * @return RestResponse Rest服务封装相应数据
     */
    public static <T> CommonResult<T> success() {
        return new CommonResult<T>();
    }


    public Boolean isSuccessful() {
        return this.code == 0;
    }
}
